/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kom;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Point2D;
import java.util.HashSet;
import kom.level.Level;
import kom.sound.Audio;

/**
 *
 * @author Nathan
 *
 */
public class InputHandler implements KeyListener, MouseListener, MouseMotionListener {

    private Level level;

    public InputHandler(Kom parent, Level level) {
        parent.addKeyListener(this);
        parent.addMouseListener(this);
        parent.addMouseMotionListener(this);
        this.level = level;
    }
    private HashSet<Integer> keysOn = new HashSet<Integer>();
    public float xMouse, yMouse;

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
//        System.out.println(e.getKeyCode());
        keysOn.add(e.getKeyCode());
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if (keysOn.contains(e.getKeyCode())) {
            keysOn.remove(e.getKeyCode());
        }
    }

    public HashSet<Integer> getKeysOn() {
        return keysOn;
    }

    public void checkPlayer(Player p, Map m) {

        if (p.isStunned()) {
            return;
        }
        if (keysOn.contains(KeyEvent.VK_SPACE) || keysOn.contains(KeyEvent.VK_W)) {
            if (p.isCanJump()) {
                if (!p.isJumping()) {
                    Audio.playSound("jump2");
                }
                p.setJumping(true);
            }
        } else {
            if (p.isJumping()) {
                p.setCanJump(false);
            }
            p.setJumping(false);
            p.setApplyGravity(true);
        }
        if (keysOn.contains(68)) {
            m.forceMoveEntity(p, Map.DIR_RIGHT);
        } else if (keysOn.contains(65)) {
            m.forceMoveEntity(p, Map.DIR_LEFT);
        } else {
            m.forceMoveEntity(p, Map.DIR_NONE);
        }
    }

    // we only using mousepressed so far
    @Override
    public void mouseClicked(MouseEvent e) {
        //comando = cJanela.verificaClick(x, y, bot);
        //String cmd = Console.windows.getClickAction((int) xMouse, (int) yMouse, e.getButton());
        //Console.handleCommand(cmd);
        //trataComando();
        calculateClickedTile(xMouse, yMouse, level);
    }

    @Override
    public void mousePressed(MouseEvent e) {
        // calculateClickedTile(xMouse, yMouse, )
        String command = Console.windows.getClickAction((int) xMouse, (int) yMouse, e.getButton());
        Console.handleCommand(command);
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        Console.windows.releaseWindow();
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
        xMouse = -1;
    }

    @Override
    public void mouseDragged(MouseEvent me) {
        xMouse = me.getX() / 2;
        yMouse = me.getY() / 2;
        Console.windows.drag((int) xMouse, (int) yMouse);
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        xMouse = e.getX() / 2;
        yMouse = e.getY() / 2;
    }
    // this is going to Mapedit window class

    private void calculateClickedTile(float xMouse, float yMouse, Level level) {
        int xTile = (int) (xMouse / 8) + Kom.screen.getOffsX() / 8;
        int yTile = (int) (yMouse / 8) + Kom.screen.getOffsY() / 8;
        Point2D pt = new Point2D.Float(xTile, yTile);
        BlockType type = null;
        try {
            type = level.getMap().getTile(pt).getBlockType();
        } catch (Throwable e) {
            return;
        }
        if (type != level.getSelectedBlock().getBlockType()) {
            level.getMap().setTile(pt, level.getSelectedBlock().getBlockType());
        }
    }
}
